/*
 * 文件名：ProductController.java
 * 版权：
 * 描述：商品处理controller  增删改查操作
 * 修改人：zuo liubo
 * 修改时间：2013-1-30 上午11:19:23
 */
package com.scpii.api.controller.ec;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scpii.api.common.annoation.Pagination;
import com.scpii.api.common.base.BaseController;
import com.scpii.api.domain.ec.Product;
import com.scpii.api.service.ec.ProductService;
import com.scpii.api.util.PrincipalUtil;

/**
 * @description 商品处理controller
 * @author zuo liubo
 * @date 2013-1-30
 */
/**
 * @author damon
 *
 */
@Controller
@RequestMapping("/ec/product")
public class ProductController extends BaseController {

	@Resource
	private ProductService productService;
	private Log logger = LogFactory.getLog(ProductController.class);

	/**
	 * 分类获取商品列表
	 * 
	 * @param product
	 * @return
	 */
	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/category/{catId}", method = RequestMethod.GET)
	@ResponseBody
	@Pagination
	public List<Product> getProductsByCatId(Product product) {
		List<Product> productList = productService.getProductsByCatId(product);
		return productList;
	}

	/**
	 * 根据产品ID来获取产品信息
	 * 
	 * @param product
	 * @return
	 */
	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
	@ResponseBody
	public Object getProductsById(Product product) {
		List<Product> productList = productService.getProductsByCatId(product);
		if (productList.size() > 0) {
			return productList;
		}
		return null;
	}
	
	
	/**
	 * 搜索产品
	 * 
	 * @param keyWord
	 * @param product
	 * @return
	 */
	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@ResponseBody
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	@Pagination
	public List<Product> searchProduct(String keyWord,
			Product product) {
		logger.info("keyWord:"+keyWord+",sortBy:"+product.getSortBy()+",sortOrder:"+product.getSortOrder());
		Long appId=PrincipalUtil.getPrincipal().getAppId();
		product.setAppId(appId);
		product.setTitle(StringUtils.isBlank(keyWord)?null:keyWord);
		product.setPublish(1);
		List<Product> productList = productService.searchProduct(product);
		if (productList.size() > 0) {
			return productList;
		}
		return null;
	}
}
